package com.sky.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.BagUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;


    /**添加菜品dish以及dishflavor表*/
    @Transactional
    @Override
    public void insertDishService(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtil.copyProperties(dto, dish);


        dishMapper.insertDish(dish);
        List<DishFlavor> dishFlavors = dish.getFlavors();
        log.info(String.valueOf("dishflavor集合数量为" + dishFlavors.size()));
        if (dishFlavors != null && dishFlavors.size() > 0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dish.getId());
//                log.info(String.valueOf(dish.getId()));
            }

            dishMapper.insertDishFlavor(dishFlavors);
        }


    }



   /** 查询菜品并进行名称分类状态进行模糊查询*/
    @Override
    public PageResult selectDishService(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        List<DishVO> dishes = dishMapper.selectDish(dto);
        Page<DishVO>  page= (Page<DishVO>) dishes;
        return new PageResult(page.getTotal(),page.getResult());
    }



   /** 根据id删除菜品（- 可以一次删除一个菜品，也可以批量删除菜品
     起售中的菜品不能删除
    被套餐关联的菜品不能删除
 删除菜品后，关联的口味数据也需要删除掉）*/
    @Transactional
    @Override
    public void deleteDishService(Integer[] ids) {
        Integer[] id=new Integer[ids.length];
        //被套餐关联的菜品不能删除
        for (int i = 0; i < ids.length; i++) {
            boolean b = dishMapper.selectSetmealDishDish(ids[i]);
            if (b){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

            }else {

                id[i]=ids[i];
            }
        }

        //起售中的菜品不能删除
        boolean b = dishMapper.deleteDish(id);

//            上面删除菜品菜品口味也要一起删除
        if (b){
            dishMapper.deleteDishflavor(id);
        }else {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

    }





 /** 进行修改先查询菜品信息包含dish表和dish_flavor表*/
    @Override
    public DishVO selectDishAndDishFlavorServise(Integer id) {
        DishVO dishVO = dishMapper.selectDishAndDishFlavor(id);
        return dishVO;

    }



   /** 修改菜品包含dish表和dish_flavor表，修改dish_flavor表先给他删除再添加就变样成了修改*/
    @Transactional
    @Override
    public void updateDishService(DishDTO dto) {
        try {
            Dish dish=new Dish();
            BeanUtil.copyProperties(dto,dish);
            dishMapper.updateDish(dish);

            if (dish.getFlavors()!=null && dish.getFlavors().size()>0){
                Integer intExact = Math.toIntExact(dish.getId());

//                删除菜品分类
                dishMapper.delectDishFlavorboolean(Math.toIntExact(dish.getId()));

                //添加菜品分类（就是修改）
                List<DishFlavor> flavors = dish.getFlavors();
                for (DishFlavor flavor : flavors) {
                    flavor.setDishId(dish.getId());
                    log.info(flavor.getName());
                }

                dishMapper.insertDishFlavor(flavors);
            }
        } catch (Exception e) {
            throw new DeletionNotAllowedException("删除失败");
        }


    }





    //在套餐管理添加菜品里查询到套餐下菜品（查询后续添加）
    @Override
    public List<Dish> selectCategoryDishService(Integer id) {
        List<Dish> dishes = dishMapper.selectCategoryDish(id);
        return dishes;
    }


    //修改菜品售卖状态
    @Override
    public void updateDishStatusService(Integer status, Integer id) {


        dishMapper.updateDishStatus(status,id);
    }
}
